package com.mioto.ld.module.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.mioto.ld.component.EditType;
import com.mioto.ld.module.product.model.ProductFunctionEditDTO;
import org.springframework.stereotype.Service;

import com.mioto.ld.module.product.dao.ProductFunctionDao;
import com.mioto.ld.module.product.model.ProductFunction;
import com.mioto.ld.module.product.service.IProductFunctionService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author mioto
 * @date 2024-05-14 21:05:43
 */
@Service("productFunctionService")
public class ProductFunctionServiceImpl implements IProductFunctionService{

    @Resource
    private ProductFunctionDao productFunctionDao;

    @Override
    public List<ProductFunction> findList(ProductFunction productFunction) {
        return productFunctionDao.findList(productFunction);
    }

    @Override
    public int insert(ProductFunction productFunction) {
        productFunction.initCreateData();
        return productFunctionDao.insertIgnoreNull(productFunction);
    }

    @Override
    public int update(ProductFunction productFunction) {
        return productFunctionDao.update(productFunction);
    }

    @Override
    public int updateIgnoreNull(ProductFunction productFunction) {
        return productFunctionDao.updateIgnoreNull(productFunction);
    }

    @Override
    public ProductFunction findByColumn(String column, Object value) {
        return productFunctionDao.findByColumn(column,value);
    }

    @Override
    public int deleteByColumn(String column, Object value) {
        return productFunctionDao.deleteByColumn(column,value);
    }

    @Override
    public int batchDelete(Integer[] ids) {
        return productFunctionDao.batchDelete(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(List<ProductFunctionEditDTO> productFunctions) {
        int result = 0;
        if (CollUtil.isNotEmpty(productFunctions)){
            result = 1;
            productFunctions.forEach(p->{
                if (p.getEditType() == EditType.EDIT_TYPE_INSERT){
                    insert(p);
                }else if (p.getEditType() == EditType.EDIT_TYPE_UPDATE){
                    updateIgnoreNull(p);
                }else if (p.getEditType() == EditType.EDIT_TYPE_DELETE){
                    deleteByColumn("id",p.getId());
                }
            });
        }
        return result;
    }
}